package com.learning.platform.repository;

import com.learning.platform.entity.Category;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CategoryRepository extends JpaRepository<Category, Long> {
    
    /**
     * 根据父分类ID查找子分类，按排序号排序
     */
    List<Category> findByParentIdOrderBySortOrder(Long parentId);
    
    /**
     * 获取顶级分类（父分类为null）
     */
    List<Category> findByParentIdIsNullOrderBySortOrder();
    
    /**
     * 查询分类及其课程数量
     */
    @Query("SELECT c, COUNT(co) FROM Category c LEFT JOIN Course co ON c.id = co.categoryId " +
           "WHERE c.parentId IS NULL GROUP BY c.id ORDER BY c.sortOrder")
    List<Object[]> findCategoriesWithCourseCount();
}


